﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WithInterfaces
{
    public class SubMenuItem : MenuItem
    {
        private readonly List<MenuItem> r_ListOfMenuItems;
        private int m_UserChosenOption = 0;
        private bool m_IsValidUserChosenOption = false;
        private int m_MinMenuValue = 0;
        private int m_MaxMenuValue = 0;

        public SubMenuItem(int i_MenuItemLevel, string i_LastChosenMenuOptionTitle, params MenuItem[] i_MenuItemParams)
        {
            m_CurrentMenuItemTitle = i_LastChosenMenuOptionTitle;
            m_MenuItemLevel = i_MenuItemLevel;
            r_ListOfMenuItems = new List<MenuItem>();

            foreach (MenuItem item in i_MenuItemParams)
            {
                r_ListOfMenuItems.Add(item);
                m_MaxMenuValue++;
            }
        }

        public override string ToString()
        {
            string str = string.Empty;

            int index = 1;

            foreach (MenuItem item in r_ListOfMenuItems)
            {
                str = string.Concat(str, item.ToString());

                if (index > 0 && index < r_ListOfMenuItems.Count)
                {
                    str = string.Concat(str, ", ");
                }

                index++;
            }

            return str;
        }

        public override void OnClick()
        {
            while (!m_IsValidUserChosenOption)
            {
                try
                {
                    printMenuItems();
                    HandleUserChosenOption();

                    if (m_UserChosenOption.Equals((int)MenuItemException.eMenuItemMessage.BackFromSubMenu))
                    {
                        throw new MenuItemException(MenuItemException.eMenuItemMessage.BackFromSubMenu);
                    }
                    else
                    {
                        r_ListOfMenuItems[m_UserChosenOption - 1].OnClick();
                    }
                }
                catch (MenuItemException e)
                {
                    if (e.MenuItemMessage.Equals(MenuItemException.eMenuItemMessage.OutOfRangeInput))
                    {
                        printInvalidInputError();
                    }
                    else if (e.MenuItemMessage.Equals(MenuItemException.eMenuItemMessage.BackFromSubMenu))
                    {
                        throw new MenuItemException(MenuItemException.eMenuItemMessage.BackFromSubMenu);
                    }
                }
                catch (Exception e)
                {
                    printInvalidInputError();
                }
            }
        }

        private void printInvalidInputError()
        {
            System.Console.Clear();
            System.Console.WriteLine("Invalid chosen option, please choose option {0} to {1} or Back", m_MinMenuValue + 1, m_MaxMenuValue);
            System.Threading.Thread.Sleep(2500);
        }

        private void HandleUserChosenOption()
        {
            string CurrentUserChosenOption = System.Console.ReadLine();

            m_UserChosenOption = int.Parse(CurrentUserChosenOption);

            if (!isValueInOptionsRange(m_UserChosenOption))
            {
                throw new MenuItemException(MenuItemException.eMenuItemMessage.OutOfRangeInput);
            }
        }

        private bool isValueInOptionsRange(int i_Value)
        {
            bool result = false;

            result = i_Value >= m_MinMenuValue && i_Value <= m_MaxMenuValue;

            return result;
        }

        private void printMenuItems()
        {
            System.Console.Clear();
            ShowTitle();
            System.Console.WriteLine("Please choose option {0} to {1} or Back" + System.Environment.NewLine, m_MinMenuValue + 1, m_MaxMenuValue);
            int index = 0;

            System.Console.Write("{0}. Back" + System.Environment.NewLine, index);
            foreach (MenuItem item in r_ListOfMenuItems)
            {
                index++;
                System.Console.Write("{0}. {1}" + System.Environment.NewLine, index, item.ToString());
            }
        }
    }
}